package com.dongdongshop.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsService;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TbSpecificationOptionService;
import com.dongdongshop.service.TbTypeTemplateService;
import com.dongdongshop.util.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.util.List;
@Controller
@RequestMapping("tbGoods")
public class TbGoodsController {
    @Reference
    private TbGoodsService tbGoodsService;
    @Reference
    private TbItemCatService tbItemCatService;
    @Reference
    private TbTypeTemplateService tbTypeTemplateService;
    @Reference
    private TbSpecificationOptionService tbSpecificationOptionService;

    @RequestMapping("toHome")
    public String toHome() {
        return "admin/home";
    }

    @RequestMapping("toshow")
    public String toGoods2() {
        System.out.println("进入方法");
        return "admin/goods";
    }

    @RequestMapping("getGoodsInfo")
    @ResponseBody
    public Result toGetInfo(TbGoods tbGoods) {
        List<TbGoods> goods = tbGoodsService.fildGoods();
        return Result.build(ConstantEnum.SUCCESS).setResult(goods);
    }

    @RequestMapping("toGoods")
    public String toGoods(Model model) {
        List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateService.selectAll();
        model.addAttribute("typeList", tbTypeTemplates);
        return "admin/goods_edit";
    }

    @RequestMapping("findItem")
    @ResponseBody
    public List<TbItemCat> fidItem(@RequestParam(defaultValue = "0") Long pid) {
        List<TbItemCat> tbItemCatList = tbItemCatService.selectById(pid);
        return tbItemCatList;
    }

    //获取类型信息,genjvcat表中的typeid查询
    @RequestMapping("findtemp")
    @ResponseBody
    public TbTypeTemplate findtemp(Long pid) {
        TbTypeTemplate tbTypeTemplates = tbItemCatService.getInfo(pid);
        return tbTypeTemplates;
    }

    //
    @RequestMapping("getSpecInfoBySpecId/{id}")
    @ResponseBody
    public List<TbSpecificationOption> getInfo(@PathVariable("id") Long id) {
        List<TbSpecificationOption> options = tbSpecificationOptionService.getOptionInfoBySpecId(id);
        return options;
    }

    @RequestMapping("upload")
    @ResponseBody
    public Result upload(@RequestParam("image") MultipartFile image) {
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tSJ9Ua3jhHftrUYpHXc";
        String accessKeySecret = "dfBwhiPS0J8MfjoFjtWorfoQDLkNbI";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 填写字符串。
        String content = "Hello OSS";

        // 创建PutObjectRequest对象。
        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        PutObjectRequest putObjectRequest
                = new PutObjectRequest("dongdongshop-zk2021", "test/test.txt",
                new ByteArrayInputStream(content.getBytes()));
// 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传字符串。
        ossClient.putObject(putObjectRequest);

// 关闭OSSClient。
        ossClient.shutdown();
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("saveGoodsInfo")
    @ResponseBody
    public Result toAdd(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, String msg) {
        tbGoods.setBrandId(1L);
        tbGoodsService.addAll(tbGoods, tbGoodsDesc, msg);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("getGoods")
    @ResponseBody
    public Result getGoods(TbGoods goods, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        System.out.println("进入方法");
        PageBean page = null;
        try {
            page = tbGoodsService.getGoods(goods, pageNum, pageSize);
            return Result.build(ConstantEnum.SUCCESS).setResult(page);
        } catch (Exception e) {
            return Result.build(ConstantEnum.ERROR);
        }
    }

    //修改审核状态
    @RequestMapping("examBatch")
    @ResponseBody
    public Result toexamBatch(@RequestParam("ids[]")Long[] ids, String num) {
        tbGoodsService.toexamBatch(ids, num);
        return Result.build(ConstantEnum.SUCCESS);
    }
    //删除
    @RequestMapping("deleteBatch{ids}")
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids") Long[] ids) {
        tbGoodsService.deleteBatch(ids);
        return Result.build(ConstantEnum.SUCCESS);
    }

}
